<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">

      <div>
        <!-- 搜索 -->
        <el-input v-model="query.blurry" clearable size="small" placeholder="模糊搜索" style="width: 170px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <div class="crud-opts">
        <span class="crud-opts-left">
          <!--左侧插槽-->
          <slot name="left" />
          <el-button size="mini" type="primary" icon="el-icon-plus" @click="assign()">分配</el-button>
          <slot name="right" />
        </span>
      </div>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="660px" append-to-body>
        <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="110px">
          <el-form-item label="姓名" prop="bisZhsqSnryXm">
            <el-input v-model="form.bisZhsqSnryXm" readonly style="width: 170px;">
              <el-button slot="append" icon="el-icon-search" title="查找患者信息" />
            </el-input>
          </el-form-item>
          <el-form-item label="身份证" prop="bisZhsqSnrySfz">
            <el-input v-model="form.bisZhsqSnrySfz" readonly style="width: 170px;" />
          </el-form-item>
          <el-form-item label="性别" prop="bisZhsqSnryXb">
            <el-input v-model="form.bisZhsqSnryXb" readonly style="width: 170px;" />
          </el-form-item>
          <el-form-item label="年龄" prop="bisZhsqNl">
            <el-input v-model="form.bisZhsqNl" readonly style="width: 170px;" />
          </el-form-item>
          <el-form-item label="医保卡号" prop="bisZhsqYbkh">
            <el-input v-model="form.bisZhsqYbkh" readonly style="width: 170px;" />
          </el-form-item>
          <el-form-item label="参保状态" prop="bisZhsqCbzt">
            <el-input v-model="form.bisZhsqCbzt" readonly style="width: 170px;" />
          </el-form-item>
          <el-form-item label="联系电话" prop="bisZhsqLxdh">
            <el-input v-model="form.bisZhsqLxdh" style="width: 170px;" />
          </el-form-item>
          <el-form-item label="地址" prop="bisZhsqHldz">
            <el-input v-model="form.bisZhsqHldz" style="width: 170px;" />
          </el-form-item>
          <el-form-item label="监护人姓名" prop="bisZhsqJhrxm">
            <el-input v-model="form.bisZhsqJhrxm" style="width: 170px;" />
          </el-form-item>
          <el-form-item label="监护人电话" prop="bisZhsqJhrdh">
            <el-input v-model="form.bisZhsqJhrdh" style="width: 170px;" />
          </el-form-item>
          <el-form-item label="关系" prop="bisZhsqGx">
            <el-select v-model="form.bisZhsqGx" filterable placeholder="请选择" style="width: 170px;">
              <el-option
                v-for="item in dict.yjsgx"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="医保所属区县" prop="bisZhsqSnryYbssqx">
            <el-select v-model="form.bisZhsqSnryYbssqx" filterable placeholder="请选择" style="width: 170px;">
              <el-option
                v-for="item in dict.ccstcqh"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="申请类别" prop="bisZhsqSqlb">
            <el-select v-model="form.bisZhsqSqlb" filterable placeholder="请选择" style="width: 170px;">
              <el-option
                v-for="item in dict.sqlb"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="申请原因">
            <el-input v-model="form.bisZhsqSqyy" :rows="3" type="textarea" style="width: 470px;" />
          </el-form-item>
          <span class="demo-image__preview">
            <el-form-item label="失能老人资料">
              <div v-for="(item, index) in this.tempFiles" :key="index">
                <a :href="item.path2" target="_blank" style="color:blue;font-size: 12px;">
                  <span v-if="item.fjlb === 'orgAdlLiveVideo'">ADL视频</span>
                </a>
              </div>
            </el-form-item>
          </span>
          <!--
          <el-form-item label="ADL量表评分" prop="bisZhsqAdl">
            <el-input v-model="form.bisZhsqAdl" readonly style="width: 170px;"/>
          </el-form-item>
          <span class="demo-image__preview">
            <el-image
              style="width: 100px; height: 100px"
              v-for="item in this.files"
              :key="item"
              :src="item"
              :preview-src-list="files">
            </el-image>
          </span>
          -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="crud.cancelCU">取消</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column align="center" prop="bisZhsqZt" label="申请状态" width="130">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.bisZhsqZt === '05'" effect="plain" type="">
              {{ dict.label.zhzt[scope.row.bisZhsqZt] }}
            </el-tag>
            <el-tag v-else-if="scope.row.bisZhsqZt === '00'" effect="plain" type="warning">
              {{ dict.label.zhzt[scope.row.bisZhsqZt] }}
            </el-tag>
            <el-tag v-else effect="plain" type="danger">
              {{ dict.label.zhzt[scope.row.bisZhsqZt] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="lrsj" label="申请时间" width="150" />
        <el-table-column align="center" prop="bisZhsqGrbh" label="人员编号" />
        <el-table-column align="center" prop="bisZhsqYbkh" label="医保卡号" width="90" />
        <el-table-column align="center" prop="bisZhsqSnryXm" label="姓名" />
        <el-table-column align="center" prop="bisZhsqSnryXb" label="性别" />
        <el-table-column align="center" prop="bisZhsqSnrySfz" label="身份证" width="150" />
        <el-table-column align="center" prop="bisZhsqCblx" label="参保类型" width="70" />
        <el-table-column align="center" prop="bisZhsqNl" label="年龄" />
        <el-table-column prop="bisZhsqDykkrq" label="待遇开始日期" width="100" />
        <el-table-column prop="bisZhsqDyjsrq" label="待遇结束日期" width="100" />
        <el-table-column align="center" prop="bisZhsqSndj" label="失能等级" width="90" />
        <el-table-column align="center" prop="bisZhsqAdl" label="ADL评分" />
        <el-table-column
          fixed="right"
          label="操作"
          width="100"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="crud.toEdit(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <dspPgry ref="dspPgry" @doQuery="doApproveQuery" />
    </div>
  </div>
</template>

<script>
import crudFile from '@/api/tools/localStorage'
import { getAdlXmxx } from '@/api/business/adlXmxx'
import dspPgry from '../hljg/dspPgry'
import crudZhsq from '@/api/business/zhsq'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'

const defaultForm = { bisZhsqId: null, bisZhsqSnryId: null, bisZhsqSnryXm: null, bisZhsqSnryXb: null, bisZhsqSnrySfz: null, bisSnryYbssqx: null, bisZhsqDykkrq: null, bisZhsqDyjsrq: null, bisZhsqXsdyq: null, bisZhsqAdl: null, bisZhsqZt: null }
export default {
  name: 'Zhsq',
  components: { pagination, rrOperation, dspPgry },
  mixins: [presenter(), header(),
    form(function() {
      return Object.assign({ hljg: { bisGsglId: this.bisHljgId }}, defaultForm)
    }), crud()],
  dicts: ['xb', 'yjsgx', 'ccs', 'zhzt', 'ccstcqh', 'sqlb'],
  cruds() {
    return CRUD({ title: '居家照护申请', url: 'api/zhsq/assigned', query: { bisHljgId: this.bisHljgId }, idField: 'bisZhsqId', sort: 'bisZhsqId,desc', crudMethod: { ...crudZhsq }})
  },
  data() {
    return {
      approveDialog: false,
      selectedZhids: [],
      bisHljgId: '',
      files: [],
      tempFiles: [],
      permission: {
        add: ['admin', 'zhsq:add'],
        edit: ['admin', 'zhsq:edit'],
        del: ['admin', 'zhsq:del']
      },
      rules: {
        bisZhsqSnryXm: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        bisZhsqSnryXb: [
          { required: true, message: '性别不能为空', trigger: 'blur' }
        ],
        bisZhsqSnrySfz: [
          { required: true, message: '身份证不能为空', trigger: 'blur' }
        ],
        bisSnryYbssqx: [
          { required: true, message: '医保所属区县不能为空', trigger: 'blur' }
        ],
        bisZhsqDykkrq: [
          { required: true, message: '待遇开始日期不能为空', trigger: 'blur' }
        ],
        bisZhsqDyjsrq: [
          { required: true, message: '待遇结束日期不能为空', trigger: 'blur' }
        ],
        bisZhsqXsdyq: [
          { required: true, message: '享受待遇期不能为空', trigger: 'blur' }
        ],
        bisZhsqAdl: [
          { required: true, message: 'ADL量表评分不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return this.approveDialog
    },
    /**
     * 点击修改之前校验
     * @param crud
     * @param form
     * @returns {boolean}
     */
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.tempFiles = []
      this.getFiles()
      return true
    },
    assign() {
      this.selectedZhids = []
      this.crud.selections.forEach((item, index) => {
        this.selectedZhids.push(item.bisZhsqId)
      })
      if (this.selectedZhids.length === 0) {
        this.$notify({
          title: '请选择待分配的照护申请',
          type: 'warning',
          duration: 2500
        })
        return
      }
      this.$refs.dspPgry.PgryDialog = 1
      this.$refs.dspPgry.selectedZhids = this.selectedZhids
    },
    doApproveQuery() {
      console.log('233333333333333333333333333')
      this.crud.toQuery()
    },
    /**
     * 打开ADL量表时，实时查询
     */
    getAdl() {
      if (this.adlIsDisabled) {
        this.$message({
          dangerouslyUseHTMLString: true,
          showClose: true,
          message: 'ADL量表已完成，修改时无法操作',
          type: 'warning'
        })
      } else {
        this.adlDrawer = true
        if (this.crud.status.add === 1 && this.crud.status.edit === 0) { // 点击新增按钮，提交的时候只是新增
          getAdlXmxx().then(res => {
            console.log(res)
            this.adlData = res.adlData
            this.form.bisZhsqAdlPc = res.adlPcId
          })
        } else if (this.crud.status.add === 0 && this.crud.status.edit === 1) {
          getAdlXmxx({ adlPcId: this.form.bisZhsqAdlPc }).then(res => {
            console.log(res)
            this.adlData = res.adlData
            this.form.bisZhsqAdlPc = res.adlPcId
          })
        }
      }
    },
    /**
     * 修改操作时，需要加载附件信息
     */
    getFiles: function() {
      crudFile.queryFileByFjlb2(this.form.bisZhsqId).then(res => {
        this.tempFiles = res
        for (let i = 0; i < res.length; i++) {
          this.files.push(res[i].path)
        }
      })
    },
    /**
     * ADL量表关闭
     * @param done
     */
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    }
  }
}
</script>

<style scoped>

</style>
